package com.ihr360.expr.data;

import com.ihr360.expr.data.util.Pair;

import java.math.RoundingMode;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ExprValueMeta {

    private static final Map<Pair<RoundingMode, Integer>, ExprValueMeta> metaCache = new ConcurrentHashMap<>();
    /**
     * 四舍五入类型
     */
    RoundingMode roundingMode;
    /**
     * 小数点保留位数
     */
    int scale;

    private ExprValueMeta(RoundingMode roundingMode, int scale) {
        this.roundingMode = roundingMode;
        this.scale = scale;
    }

    public RoundingMode getRoundingMode() {
        return roundingMode;
    }

    public int getScale() {
        return scale;
    }

    public static ExprValueMeta buildValueMeta(RoundingMode roundingMode, int scale) {
        Pair<RoundingMode, Integer> key = Pair.of(roundingMode, scale);
        ExprValueMeta value = metaCache.get(key);
        if (value == null) {
            value = new ExprValueMeta(roundingMode, scale);
            metaCache.put(key, value);
        }
        return value;
    }

}